<?php
/**
 * @author jonathan <jo.link.noir@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 13-04-2010
 */
class U1_DAO_Planet extends SW_DAO
{
	/** @var {id => SW_Planet_Element, ...}*/
	static private $planets = array();

	/** @var {id_player => {id => SW_Planet_Element, ...}, ...}*/
	static private $planetsPlayer = array();


	/**
	 * @param int $id_user
	 * @return array : {id => SW_Planet_Element, ...}
	 */
	static public function selectAll($id_user)
	{
		if (isset(self::$planetsPlayer[$id_user]))
			return self::$planetsPlayer[$id_user];

		if (false === isset(self::$queries[__METHOD__]))
		{
			self::$queries[__METHOD__] = self::$db->prepare(
				'SELECT id, galaxy, system, position, name FROM planet WHERE ref_user=?'
			);
		}

		self::$queries[__METHOD__]->execute(
			array($id_user)
		);

		self::$planetsPlayer[$id_user] = array();
		while ($o = self::$queries[__METHOD__]->fetchObject())
		{
			$o->ref_user = $id_user;
			self::$planetsPlayer[$id_user][$o->id] = self::$planets[$o->id] = self::newPlanet($o);
		}
		return self::$planetsPlayer[$id_user];
	}

	/**
	 * @param array|stdClass $coordinates : {int, int, int}
	 * @param &$realCoordinates = null : contient les coordonnées de la planète (après traitement sur $coordinates)
	 * @return SW_Planet_Element|null
	 */
	static public function selectByCoordinates($coordinates, &$realCoordinates = null)
	{
		$realCoordinates = array();

		if (count($coordinates) !== 3)
			return null;

		if (false === isset(self::$queries[__METHOD__]))
		{
			self::$queries[__METHOD__] = self::$db->prepare(
				'SELECT id, ref_user, galaxy, system, position, name FROM planet WHERE galaxy=? AND system=? AND position=? LIMIT 1'
			);
		}

		foreach ($coordinates as $v)
			$realCoordinates[] = (int)$v;

		self::$queries[__METHOD__]->execute($realCoordinates);

		return ($o = self::$queries[__METHOD__]->fetchObject())
			? (isset(self::$planets[$o->id])
				? self::$planets[$o->id]
				: self::newPlanet($o))
			: null;
	}

	/**
	 * @param int $id
	 * @return SW_Planet_Element|null
	 */
	static public function selectId($id)
	{
		if (isset(self::$planets[$id]))
			return self::$planets[$id];

		if (false === isset(self::$queries[__METHOD__]))
		{
			self::$queries[__METHOD__] = self::$db->prepare(
				'SELECT ref_user, galaxy, system, position, name FROM planet WHERE id=? LIMIT 1'
			);
		}

		self::$queries[__METHOD__]->execute(
			array($id)
		);

		if ($o = self::$queries[__METHOD__]->fetchObject())
		{
			$o->id = $id;
			return self::newPlanet($o);
		}
		else
			return self::$planets[$id] = null;
	}

	final static protected function newPlanet(StdClass $p)
	{
		$id = (int)$p->id;
		return self::$planets[$id]
				= new SW_Planet_Element($id, (int)$p->ref_user, array((int)$p->galaxy, (int)$p->system, (int)$p->position), $p->name);
	}

	/**
	 * @param int $id
	 * @param int $ref_user
	 * @return int : nombre de ligne affecté
	 */
	static public function update($id, $ref_user)
	{
		if (false === isset(self::$queries[__METHOD__]))
		{
			self::$queries[__METHOD__] = self::$db->prepare(
				'UPDATE planet SET ref_user=? WHERE id=? AND ref_user = 0'
			);
		}

		$id = (int)$id;
		$ref_user = (int)$ref_user;
		self::$queries[__METHOD__]->execute(
			array($ref_user, $id)
		);

		$rowCount = self::$queries[__METHOD__]->rowCount();

		if ($rowCount && isset(self::$planets[$id]))
		{
			///TODO unused $o
			$o = new StdClass;
			$o->id = $id;
			$o->user = $ref_user;
			list($o->galaxy, $o->galaxy, $o->galaxy) = self::$planets[$id]->getTarget();
			$o->name = '';
			if (isset(self::$planetsPlayer[$ref_user]))
				self::$planetsPlayer[$ref_user][$id] = self::$planets[$id];
		}

		return $rowCount;
	}
}